//
//  NewFeatureCollectionViewController.swift
//  weibo
//
//  Created by 陈伟 on 15/10/17.
//  Copyright © 2015年 陈伟. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewFeatureCollectionViewController: UICollectionViewController {

    init() {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = .Horizontal
        
        super.init(collectionViewLayout: layout)
        collectionView?.bounces = false
        collectionView?.backgroundColor = UIColor.redColor()
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        
        
    }
    
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //注册cell
        self.collectionView!.registerClass(NewFeatCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: UICollectionViewDataSource

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 4
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatCollectionViewCell
        
        cell.indexNo = indexPath.row
        
        return cell
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
//        print(scrollView.frame)
//        print(scrollView.contentSize)
//        print(scrollView.contentOffset)
        let page: Int = Int (scrollView.contentOffset.x / scrollView.bounds.width)
        
        if page == 3 {
            
            let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forRow: page, inSection: 0)) as! NewFeatCollectionViewCell
            cell.buttonAnim()
        }
//        scrollView.contentOffset
    }
    
}

//自定义CELL
class NewFeatCollectionViewCell: UICollectionViewCell {
    
    var indexNo: Int = 0 {
        didSet {
            startButton.hidden = true
            backImgView.image = UIImage(named: "new_feature_" + "\(indexNo + 1)")
            if indexNo+1 == 4 {
                startButton.hidden = false
            }
        }
    }
    
    @objc private func clickStartBtn() {
        print("看、 新特性")
        
        //通知window切换根控制器
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: "新特性界面发来通知")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
//        backgroundColor = UIColor.grayColor()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //button 动画
    private func buttonAnim() {
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        startButton.userInteractionEnabled = false
        
        UIView.animateWithDuration(1.6, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }, completion: { (_) -> Void in
                self.startButton.userInteractionEnabled = true
        })
    }
    //加载cell页面
    private func setupUI() {
        
        contentView.addSubview(backImgView)
        contentView.addSubview(startButton)
        startButton.addTarget(self, action: "clickStartBtn", forControlEvents: UIControlEvents.TouchUpInside)
        
        backImgView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(self.snp_size)
//            make.top.equalTo(self.snp_top)
//            make.bottom.equalTo(self.snp_bottom)
//            make.left.equalTo(self.snp_left)
//            make.right.equalTo(self.snp_right)
        }
        
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(backImgView.snp_centerX)
            make.bottom.equalTo(backImgView.snp_bottom).offset(-60)
        }
    }
    
    private lazy var backImgView = UIImageView()
    private lazy var startButton: UIButton = UIButton(title:"开始体验", color: UIColor.whiteColor(), imageName: "new_feature_finish_button")
}

